########################################################
# Project:    Commission Communication
# Task:       Plot output of Comm PRs
# Author:     Christian Rauh (16.03.2021)
########################################################

# Packages ####
library(tidyverse)
library(Hmisc)
library(cowplot)

# Load corpus
com <- read_rds("./Corpora/EC-PressReleases_1985-2020_clean.RDS")



# Monthly number of press releases ####

nmonthly <- com %>%
  select(month) %>% 
  group_by(month) %>% 
  summarise(count = n())

nmonthly$seq <- 1:nrow(nmonthly) # Sequential month number
nmonthly$january <- grepl("-01$", nmonthly$month) # Mark Januaries for plotting breaks

# Mark Commission terms
nmonthly$com <- NA
nmonthly$com[nmonthly$month < "1989-01"] <- "Delors I"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "1993-01"] <- "Delors II"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "1995-01"] <- "Delors III"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "1999-03"] <- "Santer"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "2004-12"] <- "Prodi"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "2009-11"] <- "Barroso I"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "2014-11"] <- "Barroso II"
nmonthly$com[is.na(nmonthly$com) & nmonthly$month < "2019-12"] <- "Juncker"
nmonthly$com[is.na(nmonthly$com) ] <- "VdL"
sum(is.na(nmonthly$com))

comswitches <- c("1989-01", "1993-01", "1995-01", "1999-03", "2004-12", "2009-11", "2014-11", "2019-12")
comswitches2 <- nmonthly$seq[nmonthly$month %in% comswitches]

comstarts <- c("1985-01", "1989-02", "1993-02", "1995-02", "1999-04", "2005-01", "2009-12", "2014-12", "2020-01")
comstarts2 <- nmonthly$seq[nmonthly$month %in% comstarts]

commissions <- c("Delors I", "Delors II", "Delors III", "Santer", "Prodi", "Barroso I", "Barroso II", "Juncker", "VdL")

# X labels
breaks <- nmonthly$seq[nmonthly$january]
labels <- as.character(seq(1985, 2021,1))

# Plot

dev.new()
ggplot(data = nmonthly, aes(x=seq,y=count))+
  geom_line(group = 1, size = .5, color = "grey40")+
  geom_smooth(aes(group = com), se = F, size = .9, color = "#003399")+
  geom_vline(xintercept = comswitches2, linetype = "dashed")+
  scale_x_continuous(breaks = breaks, labels = labels, expand = c(0,0))+
  scale_y_continuous(expand = c(0,5))+
  annotate("text", x = c(comstarts2)+1, y = 252, label = commissions, hjust = "left", vjust = "bottom", family = "serif")+
  labs(y = "Number of press releases issued by the European Commission\n(Absolute monthly count & smoothed trend within Commission term)\n",
       x= "")+
  theme_bw()+
  theme(text = element_text(family = "serif", size = 12),
        axis.text.x = element_text(angle = 90, vjust = .5, size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black"),
        panel.grid.minor = element_blank())

ggsave("./Plots/PR-outputTime.png", width = 34, height = 15, units = "cm")
